var bsurl = require('../../../utils/bsurl.js');
var common = require('../../../utils/util.js');
var async = require("../../../utils/async.js");
var api = require("../../../utils/api.js")
var app = getApp();
Page({
  data: {
    banner: null,
    loading: true,
    titleBar: [],
  },
  toggleplay: function() {

  },
  onLoad: function(options) {
    this.setTitleBar();
    if (options.share == 1) {
      var url = '../' + options.st + '/index?id=' + options.id
      wx.navigateTo({
        url: url,
        success: function() {}
      })
      return;
    }
    this.setData({
      screenHeight: app.globalData.systemInfo.screenHeight
    })
  },
  onHide: function() {

  },
  onShow: function() {
    var that = this
    app.registerPage(that)
    if (app.globalData.tabidx) {
      that.data.tabidx = app.globalData.tabidx
    }
    that.switchTab();
    that.setData({
      music: app.globalData.music,
      idx: app.globalData.index,
    })
    that.updatePlayInfo()
  },
  switchTab: function(e) {
    var that = this;
    that.setData({
      loading: true,
    })
    if (that.data.tabidx == undefined) {
      wx.request({
        url: api.bannerList,
        success: function(res) {
          that.setData({
            banner: res.data.dataList,
          })
          that.setData({
            tabidx: 0
          })
          that.getAlbumList()
        }
      })
    } else {
      let tid = 0
      if (e != undefined) {
        tid = e.currentTarget.dataset.t
      } else {
        tid = that.data.tabidx
      }
      that.setData({
        tabidx: tid
      })
      that.getAlbumList()
    }

  },
  setTitleBar: function() {
    var that = this
    wx.request({
      url: api.getTitle,
      success:(res)=>{
        that.setData({
          titleBar: res.data
        })
      }
    })
  },
  getAlbumList: function() {
    //分类歌单列表
    var that = this
    app.globalData.tabidx = that.data.tabidx
    var tid = that.data.tabidx
    that.setData({
      scrollTop: 0,
    })
    if (tid != 0) {
      wx.request({
        url: api.typeList + "?category=" + tid,
        success: function(res) {
          if (res.data.code == 1001) {
            let data = res.data.dataList;
            let mSet = []
            for (var i in data) {
              mSet.push(data[i].id)
            }
            async.map(data, function(item, callback) {
              wx.request({
                url: api.albumList + "?type=" + item.id,
                success: function(res) {
                  res.data.type = item.id
                  res.data.title = item.title
                  callback(null, res.data)
                }
              })
            }, function(err, results) {
              that.setData({
                loading: false,
                albumList: results
              })
            });
          }
        }
      })
    } else {
      async.map([-1, 1, 2, 3], function(item, callback) {
        wx.request({
          url: api.recomendList + "?category=" + item,
          //data: { cookie: app.globalData.cookie },
          success: function(res) {
            callback(null, res.data.dataList)
          }
        })
      }, function(err, results) {
        that.setData({
          loading: false,
          albumList: results
        })
      })
    }
  },
  updatePlayInfo: function() {
    var that = this
    var playInfo = app.getPlayInfo()
    that.setData({
      playing: !playInfo.paused
    })
  },
  contiune: function() {
    var that = this
    app.seek(() => {}, -1)
  },
  pause: function() {
    var that = this
    app.pause(() => {})
  },
  toggleMusic: function(e) {
    var that = this
    app.toggleMusic(() => {
      that.setData({
        music: app.globalData.music
      })
    }, e.currentTarget.dataset.shift, e.currentTarget.dataset.trigger)
  },
})